<?
class EditProvinceForm extends Form
{
	private $province=array();
	
	function EditProvinceForm()
	{
		Form::Form('EditProvinceForm');
                $this->link_js("javascript/jquery/jquery.ui.core.js");
                $this->link_js("javascript/jquery/jquery.ui.widget.js");
                $this->link_js("javascript/jquery/jquery.ui.mouse.js");
                $this->link_js("javascript/jquery/jquery.ui.sortable.js");
                $this->link_js("javascript/tiny_mce/jquery.tinymce.js");
                $this->link_js("javascript/tiny_mce/tiny_mce.js");


                //$this->link_css('style/jquery-ui-1.9.2.custom.css');
                $this->link_css('style/msgBoxLight.css');
                //$this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
                $this->link_js("javascript/jquery/jquery.msgBox.js");
                $this->link_js("javascript/hotelform.js");

                $this->link_outside_js("http://maps.google.com/maps/api/js?sensor=false");
                $this->link_js("javascript/map/geocoder_tool.js");
                $this->link_css("style/map/geocoder_tool.css");
		if(Url::get('cmd')=='edit'){			
			$id = (int)Url::get('id');
			if($id){
				$this->province=DB::select('location','id='.$id);
			}
			
			if(!$this->province)
			Url::redirect_current();
		}
		else{
			$this->province=array('name'=>'','id_citiesfather'=>0,'id_area'=>0,'orders'=>0,'active'=>1);
		}
	}
	
	function on_submit(){
		$location["name"]		= Url::getParam('name');
        $location["id_citiesfather"]= Url::getInt('id_citiesfather',0);
        $location["id_area"]	= Url::getInt('id_area',0);
        $location["latitude"]	= Url::get('hotel_latitude',0);
        $location["longitude"]	= Url::get('hotel_longitude',0);
        if($location["id_citiesfather"]!=0){
            $location["id_area"]	= 0;
        }
        $location["orders"]	= Url::getInt('orders',0);
        $location["active"]		= Url::getInt('active',0);

        $valid = true;
        if($location["name"]==''){
            $valid = false;
            $message = "Tên tỉnh thành không được để trống!";
        }
		if($valid){
                    
                    if(isset($_FILES["file_up"])){
                        $avatar = $_FILES["file_up"]["name"];
                        if($_FILES["file_up"]["tmp_name"]){
                        if(Upload::storage_check_dir("provice/", true, IMAGE_SERVER_NO))
                        {
                            $img_ext = explode(".", $avatar);
                            $file_ext = array_pop($img_ext);
                            if(!in_array($file_ext,array('jpg','png','jpeg','gif'))){
                                $this->setFormError("error_file_ext","File tải lên không đúng định dạng!");
                                return ;
                            }
                            $file_name = UP_FOLDER."/provice/" . String::_name_cleaner(Unicode::stripUnicode($location["name"]),'-').'.'.$file_ext ;
                            $sourceName = $_FILES["file_up"]["tmp_name"];
                            if(!Upload::storage_put_file($file_name, $sourceName, IMAGE_SERVER_NO, FTP_BINARY,1)){
                                $this->setFormError("error_upload_file","Có lỗi xảy ra khi upload file!");
                                return ;
                            };
                            $location['img_url'] = $file_name;
                            $location['img_server'] = IMAGE_SERVER_NO;
                        }
                    }
                }
                    
			$id = Url::get("id");
            if($id==''){
                $check = Db_Location::insert($location);
            }else{
                $check = Db_Location::update($location,$id);
            }
            if($check){
                //Thanh cong
                $this->setFormSucces("success","Save hành công!");
            }else{
                $this->setFormError("message","Có lỗi xảy ra");
            }
		}else{
            $this->setFormError("message",$message);
        }
	}	
	
	function draw()
	{	
		global $display;
		$this->beginForm(TRUE);
		$display->add('name',Url::get('name',$this->province['name']));
        $display->add('citiesfather', Db_Location::getLocationByParentId(0));
        $display->add('id_citiesfather',Url::get('id_citiesfather',$this->province['id_citiesfather']));
        $display->add('area', array(1=>'Bắc',2=>'Trung',3=>'Nam'));
        $display->add("id_area",Url::get('id_area',$this->province['id_area']));

        $display->add('orders',Url::get('orders',$this->province['orders']));
        $display->add('active_data', array(1=>'Hiện',0=>'Ẩn'));
        $display->add('active',Url::get('active',$this->province['active']));
        if(Url::get('cmd')=='edit'){
            $display->add('loca_longitude',  Url::get('hotel_longitude',$this->province['longitude']));
            $display->add('loca_latitude',  Url::get('hotel_latitude', $this->province['latitude']));
            $display->add("avatar",Db_Provider::getImageUrl( $this->province['img_url']));
            $display->add('mode',"SỬA TỈNH THÀNH");
            $display->add('title_submit',"Sửa");
        }
        else
        {
            $display->add('mode',"THÊM TỈNH THÀNH");
            $display->add('title_submit',"Thêm");
            
            if($this->showFormSuccesMessages(1)!='')//submit thành công
            {
                $display->add('name','');
                $display->add('citiesfather', Db_Location::getLocationByParentId(0));
                $display->add('id_citiesfather',0);
                $display->add('area', array(1=>'Bắc',2=>'Trung',3=>'Nam'));
                $display->add("id_area",1);
                $display->add('orders',0);
                $display->add('active_data', array(1=>'Hiện',0=>'Ẩn'));
                $display->add('active',1);
            }
        }
        
        $display->add('message',$this->showFormErrorMessages(1));
        $display->add("success",$this->showFormSuccesMessages(1));

        $display->output('ProvinceEdit');
		$this->endForm();
	}
}
?>